package com.ouzexi.finalexp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.Cancel
import kotlinx.android.synthetic.main.activity_detail.*
import okhttp3.*
import java.io.IOException

class addAdmire : AppCompatActivity() {
    lateinit var token:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_admire)

        token = save().getData(this,"token")

        val id = intent.getLongExtra("jid",0)

        save().saveData(this,"id",id.toString())

        val jname = intent.getStringExtra("jname")
        val jtype = intent.getStringExtra("jtype")
        val jpay = intent.getIntExtra("jpay",0)
        val jst = intent.getStringExtra("jst")
        val jdemand = intent.getStringExtra("jdemand")
        val jresponsibility = intent.getStringExtra("jresponsibility")
        val jename = intent.getStringExtra("jename")
        val jperiod = intent.getStringExtra("jperiod")

        tv_position_name.text = jname
        tv_position_type.text = jtype
        tv_position_salary.text = jpay.toString()
        tv_salary_type.text = jst
        tv_edu_requirement.text = jdemand
        tv_work_exp_requirement.text = jresponsibility
        tv_work_area.text = jename
        tv_work_description.text = jperiod

    }

    fun submit(view: View) {
        val id = save().getData(this,"id")

        Log.i("test",id.toString())

        val urlAPI = "http://10.0.2.2:8080/jobs/favor-position/favorPosition?jpid=${id}"
        Log.i("minetoken", token)
        val request = Request.Builder()
            .url(urlAPI)
            .addHeader("token", token)
            .get()
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                val cancel = Gson().fromJson(result, Cancel::class.java)
                runOnUiThread{
                    if (cancel.code == -1) {
                        Toast.makeText(this@addAdmire,"添加失败", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(this@addAdmire,"添加成功", Toast.LENGTH_SHORT).show()
                    }
                }
            }
            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })
    }
}